package com.hehui.module.goods.service.impl;

import com.hehui.module.goods.dao.GoodsCategoryMapper;
import com.hehui.module.goods.entity.GoodsCategory;
import com.hehui.module.goods.entity.GoodsCategoryExample;
import com.hehui.module.goods.service.IGoodsCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商品分类Service
 */
@Service
public class GoodsCategoryServiceImpl implements IGoodsCategoryService {

    @Autowired
    GoodsCategoryMapper goodsCategoryMapper; // 商品分类mapper
    @Autowired
    BaseGoodsMapper goodsMapper; // 商品mapper

    @Override
    public int delGoodsCategoryById(Long categoryId) {
        return goodsCategoryMapper.deleteByPrimaryKey(categoryId);
    }

    //  修改商品信息关联的分类id
    @Override
    public int upBaseGoodsCategoryId(BaseGoods baseGoods) {
        return goodsMapper.updateByPrimaryKeySelective(baseGoods);
    }

    // 根据分类父id查询分类
    @Override
    public List<GoodsCategory> findGoodsCategoryByParentId(Long parentId) {
        GoodsCategoryExample example = new GoodsCategoryExample();
        // 默认查询 is_del 字段为1
        example.createCriteria().andIsDelEqualTo("1").andGcParentidEqualTo(parentId);
        return goodsCategoryMapper.selectByExample(example);
    }

    // 通过分类名查询分类 带商户id
    @Override
    public List<GoodsCategory> findCateGoryGcName(String gcName,Long merchantId) {
        GoodsCategoryExample example = new GoodsCategoryExample();
        example.createCriteria().andGcNameEqualTo(gcName).andGcMerchantidEqualTo(merchantId);
        return goodsCategoryMapper.selectByExample(example);
    }

    // 添加分类
    @Override
    public int addGoodsCategory(GoodsCategory goodsCategory) {
        return goodsCategoryMapper.insertSelective(goodsCategory);
    }

    // 根据分类上级id查询分类等级  分类上级id就等于父id 等同于 通过主键id查询
    @Override
    public String findCateGoryGcLevelByParentId(Long gcParentId) {

        return goodsCategoryMapper.selectByPrimaryKey(gcParentId).getGcLevel();
    }

    // 修改商品分类
    @Override
    public int upGoodsCateGory(GoodsCategory goodsCategory) {
        return goodsCategoryMapper.updateByPrimaryKeySelective(goodsCategory);
    }

    // 根据主键id查询分类信息
    @Override
    public GoodsCategory findGoodsCategoryById(Long categoryId) {
        return goodsCategoryMapper.selectByPrimaryKey(categoryId);
    }

    // 修改默认值
    @Override
    public int upGoodsCateGoryIsDef(GoodsCategory category) {
        // 设置默认值 is_def 为 “0”
        category.setIsDef("0");
        return goodsCategoryMapper.updateByPrimaryKeySelective(category);
    }

    // 根据商品分类id查询出商品表对应商品
    @Override
    public List<BaseGoods> findBaseGoodsBycategoryId(Long categoryId) {
        BaseGoodsExample example = new BaseGoodsExample();
        example.createCriteria().andBgGoodcategoryidEqualTo(categoryId);
        return goodsMapper.selectByExample(example);
    }

    // 根据默认值查询分类 带商户id
    @Override
    public List<GoodsCategory> findCateGoryByIsDef(String isDef,Long merchantId) {
        GoodsCategoryExample example = new GoodsCategoryExample();
        example.createCriteria().andIsDefEqualTo(isDef).andGcMerchantidEqualTo(merchantId);
        return goodsCategoryMapper.selectByExample(example);
    }

    // 通过分类编号查询分类 带商户编号
    @Override
    public List<GoodsCategory> findCateGoryGcNum(String gcNum,Long merchantId) {
        GoodsCategoryExample example = new GoodsCategoryExample();
        example.createCriteria().andGcNumEqualTo(gcNum).andGcMerchantidEqualTo(merchantId);
        return goodsCategoryMapper.selectByExample(example);
    }

    // 查询分类编号最大值 带商户编号
    @Override
    public String goodsCategoryFindMaxNum(Long merchantId) {
        return goodsCategoryMapper.goodsCategoryFindMaxNum(merchantId);
    }

    // 展开和收起 (等于查询所有 未删除状态的分类) 带商户id
    @Override
    public List<GoodsCategory> findGoodsCategory(Long merchantId) {
        GoodsCategoryExample example = new GoodsCategoryExample();
        // 默认查询 id_del 为 1
        example.createCriteria().andIsDelEqualTo("1").andGcMerchantidEqualTo(merchantId);
        return goodsCategoryMapper.selectByExample(example);
    }

    // 查询所有默认顶级分类 带商户id
    @Override
    public List<GoodsCategory> findGoodsCategoryTopAll(Long merchantId) {
        GoodsCategoryExample example = new GoodsCategoryExample();
        // 查询 id_del 为 1 代表未删除状态，顶级分类id为 0L
        example.createCriteria().andIsDelEqualTo("1").andGcParentidEqualTo(0L).andGcMerchantidEqualTo(merchantId);
        return goodsCategoryMapper.selectByExample(example);
    }
}
